package com.cpecc.controller.wuzi;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.mail.SendMailThread;
import com.cpecc.mail.SendMailThread1;
import com.cpecc.manager.system.DepManager;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BuPeiJLManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.PanDianManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoJLManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.BuPei;
import com.cpecc.model.wuzi.BuPeiJL;
import com.cpecc.model.wuzi.PanDian;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;

@Controller
@RequestMapping(value = "/wuzi")
public class PanDianController {
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private PanDianManager panDianManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired	
	private YiJiaoJLManager yiJiaoJLManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private BuPeiJLManager buPeiJLManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private RootManager rootManager;
	
	@RequestMapping(value = "/ziChanPanDian")
	public ModelAndView ziChanPanDian(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("pandian/ziChanPanDian");
		return mav;
	}
	
	@RequestMapping(value = "/ziChanPanDianCX")
	public ModelAndView ziChanPanDianCX(WuZi wuZi,@RequestParam(required=false)String depId,@RequestParam(required=false)String userId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("yiChangList", wuZiManager.getYiChangList(wuZi,depId,userId));
		mav.setViewName("pandian/ziChanPanDianListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/ziChanPanDianCXXQB")
	public ModelAndView ziChanPanDianCXXQB(WuZi wuZi,@RequestParam(required=false)String depId,@RequestParam(required=false)String userId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiList", wuZiManager.getYiChangList(wuZi,depId,userId));
		mav.setViewName("wuzi/wuZiXQB");
		return mav;
	}
	
	@RequestMapping(value = "/stopPanDian")
	public void stopPanDian(HttpServletResponse response){
		wuZiManager.stopPanDian();
		List<WuZi> stopList=wuZiManager.listStop();
		for(WuZi wuZi:stopList){
			toDoManager.deleteList(wuZi);
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/danDuPanDian")
	public void danDuPanDian(String id,HttpServletResponse response) {
		WuZi wuZi=wuZiManager.get(id);
		User user=wuZi.getUser();
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setTask("资产盘点");
		toDo.setType("资产盘点");
		toDo.setUser(wuZi.getUser());
		toDo.setWuZi(wuZi);
		toDoManager.save(toDo);
		wuZiManager.danDuPanDian(id);
		Email email=emailManager.getEmail();
		YouJian youJian=youJianManager.get(3);
		SendMailThread1 st=new SendMailThread1(email, user, youJian, wuZi);
		Thread t=new Thread(st);
		st.setRoot(rootManager.getRoot());
		t.start();
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/faQiPanDian")
	public void faQiPanDian(HttpServletResponse response) {
		wuZiManager.quChuDiuShi();
		Email email=emailManager.getEmail();
		List<WuZi> wuZis=wuZiManager.getPanDianWuZi();
		for(WuZi wuZi:wuZis){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setTask("资产盘点");
			toDo.setType("资产盘点");
			toDo.setUser(wuZi.getUser());
			toDo.setWuZi(wuZi);
			toDoManager.save(toDo);
			YouJian youJian=youJianManager.get(3);
			SendMailThread1 st=new SendMailThread1(email, wuZi.getUser(), youJian, wuZi);
			st.setRoot(rootManager.getRoot());
			Thread t=new Thread(st);
			t.start();
		}
		wuZiManager.faQiPanDian();
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/panDianDaiBan")
	public ModelAndView panDianDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		int active=0;
		mav.addObject("panDianList", wuZiManager.getMyPanDian(user));
		if(wuZiManager.getMyPanDian(user).size()>0)
			active=1;
		if(currentUser.hasRole("低值管理员")){
			mav.addObject("suoPeiList", buPeiManager.getBuPeiList(802));
			if(active==0)
				if(buPeiManager.getBuPeiList(802).size()>0)
					active=2;
			mav.addObject("peiChangRZ", buPeiManager.getBuPeiList(805));
			if(active==0)
				if(buPeiManager.getBuPeiList(805).size()>0)
					active=5;
		}
		mav.addObject("peiChangTX", buPeiManager.getMyBuPeiList(user, 803));
		if(active==0)
			if(buPeiManager.getMyBuPeiList(user, 803).size()>0)
				active=3;
		if(currentUser.hasRole("财务")){
			mav.addObject("shouJiaoCZ", buPeiManager.getBuPeiList(804));
			if(active==0)
				if(buPeiManager.getBuPeiList(804).size()>0)
					active=4;
		}
		mav.addObject("active", active);
		mav.setViewName("pandian/panDianDaiBan");
		return mav;
	}
	
	@RequestMapping(value = "/getPanDianShu")
	public void getPanDianShu(HttpServletResponse response){
		int zongShu=0;
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		if(currentUser.hasRole("低值管理员")){
			zongShu+=buPeiManager.getBuPeiList(802).size();
			zongShu+=buPeiManager.getBuPeiList(805).size();
		}
		if(currentUser.hasRole("财务")){
			zongShu+=buPeiManager.getBuPeiList(804).size();
		}
		zongShu+=wuZiManager.getMyPanDian(user).size();
		zongShu+=buPeiManager.getMyBuPeiList(user, 803).size();
		response.setContentType("application/json;charset=utf-8");		
		String result="{\"result\":\"("+zongShu+")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/panDianQueRen")
	public void panDianQueRen(String id,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(id);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		PanDian panDian=new PanDian();
		panDian.setFanKuiR(user);
		panDian.setFanKuiRQ(new Date());
		panDian.setFaQiRQ(wuZi.getPanDianRQ());
		panDian.setWuZi(wuZi);
		panDian.setPanDianJieGuo("盘点确认");
		panDianManager.save(panDian);
		wuZi.setPanDian(0);
		wuZiManager.cover(wuZi);
		toDoManager.deleteList(wuZi);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/buBanYiJiaoSQ")
	public ModelAndView buBanYiJiaoSQ(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZi", wuZiManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("pandian/yiJiao");
		return mav;
	}
	
	@RequestMapping(value = "/panDianYiJiaoSave")
	public void yiJiaoSave(String wuZiId,String xinCunFangD,String depId,String userId,String yiJiaoYY,HttpServletResponse response){
		if(weiXiuManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(baoFeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		WuZi wuZi=wuZiManager.get(wuZiId);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=new YiJiao();
		yiJiao.setBuBan(0);
		yiJiao.setHuanJie(huanJieManager.get(502));
		yiJiao.setJieShouDW(depManager.get(depId));
		yiJiao.setJieShouR(userManager.getUser(userId));
		yiJiao.setLiuChengLX(2);
		yiJiao.setShenQingRQ(new Date());
		yiJiao.setWuZi(wuZi);
		yiJiao.setYiJiaoR(user);
		yiJiao.setYiJiaoYY(yiJiaoYY);
		yiJiao.setBuBan(1);
		yiJiaoManager.save(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(501));
		yiJiaoJL.setPiFuLX("原使用人移交");
		yiJiaoJL.setPiFuNR("补办移交");
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		PanDian panDian=new PanDian();
		panDian.setFanKuiR(user);
		panDian.setFanKuiRQ(new Date());
		panDian.setFaQiRQ(wuZi.getPanDianRQ());
		panDian.setWuZi(wuZi);
		panDian.setPanDianJieGuo("补办移交");
		panDian.setYiJiao(yiJiao);
		panDianManager.save(panDian);
		wuZi.setPanDian(3);
		wuZiManager.cover(wuZi);
		toDoManager.deleteList(wuZi);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(502));
			toDo.setTask("移交审批");
			toDo.setType("移交");
			toDo.setUser(user1);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(502);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 502, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/panDianDiuShi")
	public void panDianDiuShi(String id,HttpServletResponse response){
		if(weiXiuManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}			
		else if(yiJiaoManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(baoFeiManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(id)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		WuZi wuZi=wuZiManager.get(id);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BuPei buPei=new BuPei();
		buPei.setHuanJie(huanJieManager.get(802));
		buPei.setUser(user);
		buPei.setWuZi(wuZi);
		buPeiManager.save(buPei);
		BuPeiJL buPeiJL=new BuPeiJL();
		buPeiJL.setBuPei(buPei);
		buPeiJL.setCaoZuoRQ(new Date());
		buPeiJL.setHuanJie(huanJieManager.get(801));
		buPeiJL.setPiFuLX("使用人确认丢失");
		buPeiJL.setUser(user);
		buPeiJLManager.save(buPeiJL);
		PanDian panDian=new PanDian();
		panDian.setBuPei(buPei);
		panDian.setFanKuiR(user);
		panDian.setFanKuiRQ(new Date());
		panDian.setFaQiRQ(wuZi.getPanDianRQ());
		panDian.setWuZi(wuZi);
		panDianManager.save(panDian);
		wuZi.setPanDian(2);
		wuZiManager.cover(wuZi);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		toDoManager.deleteList(wuZi);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(802));
			toDo.setTask("索赔");
			toDo.setType("资产盘点");
			toDo.setUser(user1);
			toDo.setWuZi(wuZi);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(802);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 802, user1, youJian);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/spList")
	public ModelAndView spList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("suoPeiList", buPeiManager.getBuPeiList(802));
		mav.setViewName("pandian/spListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/suoPei")
	public ModelAndView suoPei(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("buPei", buPeiManager.get(id));
		mav.setViewName("pandian/suoPei");
		return mav;
	}
	
	@RequestMapping(value = "/suoPeiTJ")
	public void suoPeiTJ(String id,double peiChangJE,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BuPei buPei=buPeiManager.get(id);
		buPei.setHuanJie(huanJieManager.get(803));
		buPei.setPeiChangJE(peiChangJE);
		buPeiManager.cover(buPei);
		BuPeiJL buPeiJL=new BuPeiJL();
		buPeiJL.setBuPei(buPei);
		buPeiJL.setCaoZuoRQ(new Date());
		buPeiJL.setHuanJie(huanJieManager.get(802));
		buPeiJL.setPiFuLX("低值管理员索赔");
		buPeiJL.setUser(user);
		buPeiJLManager.save(buPeiJL);
		
		toDoManager.deleteList(buPei.getWuZi());
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(803));
		toDo.setTask("索赔确认");
		toDo.setType("赔偿");
		toDo.setUser(buPei.getUser());
		toDo.setWuZi(buPei.getWuZi());
		toDoManager.save(toDo);
		//\\发送邮件
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		YouJian youJian=youJianManager.get(803);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 803, buPei.getUser(), youJian);
			st.setWuZi(buPei.getWuZi());
			st.setBuPei(buPei);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/spqrList")
	public ModelAndView spqrList(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		mav.addObject("suoPeiList", buPeiManager.getMyBuPeiList(user, 803));
		mav.setViewName("pandian/spqrListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/shangJiaoCZ")
	public void shangJiaoCZ(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BuPei buPei=buPeiManager.get(id);
		buPei.setHuanJie(huanJieManager.get(804));
		buPeiManager.cover(buPei);
		BuPeiJL buPeiJL=new BuPeiJL();
		buPeiJL.setBuPei(buPei);
		buPeiJL.setCaoZuoRQ(new Date());
		buPeiJL.setHuanJie(huanJieManager.get(803));
		buPeiJL.setPiFuLX("使用人赔偿");
		buPeiJL.setUser(user);
		buPeiJLManager.save(buPeiJL);
		
		//财务收缴残值
		toDoManager.deleteList(buPei.getWuZi());
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getAllCaiWuR()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(804));
			toDo.setTask("赔偿入账");
			toDo.setType("赔偿");
			toDo.setUser(user1);
			toDo.setWuZi(buPei.getWuZi());
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(804);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 804, user1, youJian);
				st.setWuZi(buPei.getWuZi());
				st.setBuPei(buPei);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/sjczList")
	public ModelAndView sjczList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shouJiaoCZ", buPeiManager.getBuPeiList(804));
		mav.setViewName("pandian/sjczListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/peiChangRZ")
	public void peiChangRZ(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BuPei buPei=buPeiManager.get(id);
		buPei.setHuanJie(huanJieManager.get(805));
		buPei.setWanChengRQ(new Date());
		buPeiManager.cover(buPei);
		BuPeiJL buPeiJL=new BuPeiJL();
		buPeiJL.setBuPei(buPei);
		buPeiJL.setCaoZuoRQ(new Date());
		buPeiJL.setHuanJie(huanJieManager.get(804));
		buPeiJL.setPiFuLX("赔偿确认");
		buPeiJL.setPiFuNR("确认");
		buPeiJL.setUser(user);
		buPeiJLManager.save(buPeiJL);
		WuZi wuZi=buPei.getWuZi();
		wuZi.setPanDian(4);
		wuZi.setShiYongZT(shiYongZTManager.get(110));
		wuZiManager.cover(wuZi);
		
		toDoManager.deleteList(buPei.getWuZi());
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(805));
			toDo.setTask("赔偿确认");
			toDo.setType("赔偿");
			toDo.setUser(user1);
			toDo.setWuZi(buPei.getWuZi());
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(805);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 805, user1, youJian);
				st.setWuZi(buPei.getWuZi());
				st.setBuPei(buPei);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/pcqrList")
	public ModelAndView pcqrList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("peiChangRZ", buPeiManager.getBuPeiList(805));
		mav.setViewName("pandian/pcqrListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/peiChangQR")
	public void peiChangQR(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		BuPei buPei=buPeiManager.get(id);
		buPei.setHuanJie(huanJieManager.get(806));
		buPei.setWanChengRQ(new Date());
		buPeiManager.cover(buPei);
		BuPeiJL buPeiJL=new BuPeiJL();
		buPeiJL.setBuPei(buPei);
		buPeiJL.setCaoZuoRQ(new Date());
		buPeiJL.setHuanJie(huanJieManager.get(805));
		buPeiJL.setPiFuLX("赔偿记账");
		buPeiJL.setPiFuNR("入账");
		buPeiJL.setUser(user);
		buPeiJLManager.save(buPeiJL);
		WuZi wuZi=buPei.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(110));
		wuZiManager.cover(wuZi);
		toDoManager.deleteList(buPei.getWuZi());
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/peiChangDanJu")
	public void peiChangDanJu(String id,HttpServletResponse response){
		BuPei buPei=buPeiManager.get(id);
		WuZi wuZi=buPei.getWuZi();
		response.setContentType("application/json;charset=utf-8");		
		String format="yyyy-MM-dd";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		String danJuRQ=sdf.format(new Date());
		String bFziChanBH=wuZi.getZiChanBH();
		String bFziChanBM=wuZi.getZiChanBM();
		String bFwuZiMC=wuZi.getWuZiMC();
		String bFgouMaiRQ=sdf.format(wuZi.getGouMaiRQ());
		String bFkaiShiJiTiRQ="";
		try {			
			bFkaiShiJiTiRQ=sdf.format(wuZi.getKaiShiJiTiRQ());
		} catch (Exception e) {
			bFkaiShiJiTiRQ="";
		}		
		DecimalFormat df=new DecimalFormat("#############0.00");
		String bFwuZiYZ=df.format(wuZi.getWuZiYZ());
		String bFwuZiJingZhi=df.format(wuZi.getWuZiJingZhi());
		String bFleiJiZheJiuJE=df.format(wuZi.getLeiJiZJJE());
		String peiChangJE=df.format(buPei.getPeiChangJE());
		String bFzheJiuYX=String.valueOf(wuZi.getZheJiuYX());
		String bFyiZheJiuYS=String.valueOf(wuZi.getZheJiuYS());
		String bFshiYongBM=wuZi.getDep().getDepName();
		String bFshiYongR=wuZi.getUser().getName();		
		String result="{\"danJuRQ\":\""+danJuRQ+"\",\"bFziChanBH\":\""+bFziChanBH+"\",\"bFziChanBM\":\""
				+bFziChanBM+"\",\"bFwuZiMC\":\""+bFwuZiMC+"\",\"bFgouMaiRQ\":\""+bFgouMaiRQ
				+"\",\"bFkaiShiJiTiRQ\":\""+bFkaiShiJiTiRQ+"\",\"bFwuZiYZ\":\""+bFwuZiYZ
				+"\",\"bFwuZiJingZhi\":\""+bFwuZiJingZhi+"\",\"bFleiJiZheJiuJE\":\""+bFleiJiZheJiuJE
				+"\",\"bFzheJiuYX\":\""+bFzheJiuYX+"\",\"bFyiZheJiuYS\":\""+bFyiZheJiuYS
				+"\",\"bFshiYongBM\":\""+bFshiYongBM+"\",\"bFshiYongR\":\""+bFshiYongR
				+"\",\"peiChangJE\":\""+peiChangJE+"\"}";		
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
